#!/bin/bash

echo "#########################卸载旧版本docker#########################"
sudo service docker stop
sudo apt-get purge docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
sudo rm -rf /etc/docker/daemon.json
sudo apt-get update

echo "#########################设置docker存储库#########################"
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
          "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg]  https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
            $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

echo "#########################开始安装docker#########################"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

echo "#########################配置国内源#########################"
cat >> /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": [
            "http://hub-mirror.c.163.com",
            "https://3oo2g4yy.mirror.aliyuncs.com",
            "https://docker.mirrors.ustc.edu.cn",
            "https://registry.docker-cn.com",
            "https://registry.docker.com",
            "https://registry.docker-cn.com"
    ],
    "exec-opts":["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "100m"
    },
    "storage-driver": "overlay2"
}
EOF

echo "#########################重启docker#########################"
echo "service docker restart"
echo "#########################安装完成#########################"
echo "安装完成,请执行: docker info 验证"
docker info